package interaction.end;

import com.hzheima.util.ImageUtil;
import constant.EndConstant;
import constant.UiConstant;
import entity.Boss;
import entity.Plane;
import interaction.crash.EntityCrash;
import ui.PlaneWarPanel;
import util.KeyStatesConfig;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/**
 * @Author:zhengbowen
 * @Date: 2025/09/09 16:22
 * @Description:处理游戏结束逻辑
 */
public class EndLogic {
    //游戏初始为未结束
    public static boolean gameOver = false;
    public static boolean gameWin = false;

    //结束之后飞机改变图片
    public static void changePlaneImag(Plane plane) {
        BufferedImage img = ImageUtil.getImg("boom.png");
        plane.setImage(img);
    }

    //游戏结束了绘制提示
    public static void gameOverUi(Graphics g) {
        if (gameOver && !gameWin) {
            //设置画笔为红色
            g.setColor(Color.red);
            Graphics2D g2d = (Graphics2D) g;
            //设置透明度,绘制红色矩形
            AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
            g2d.setComposite(alphaComposite);
            g2d.fillRect(0, 200, UiConstant.APP_WIDTH, 300);
            //透明度设置回来，然后设置成白色
            AlphaComposite alphaComposite1 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
            g2d.setComposite(alphaComposite1);
            g.setColor(Color.white);
            g.setFont(new Font("楷体", Font.BOLD, 30));
            g.drawString("游戏结束", UiConstant.APP_WIDTH / 2 - 100, 240);
            g.setColor(Color.yellow);
            g.drawString("本次得分：" + EntityCrash.score, UiConstant.APP_WIDTH / 2 - 100, 300);
            g.drawString("获得金币：", UiConstant.APP_WIDTH / 2 - 100, 370);
            g.drawImage(PlaneWarPanel.MoneyImage, UiConstant.APP_WIDTH / 2 - 70, 340, 40, 40, null);
            g.drawString("0.00k", UiConstant.APP_WIDTH / 2 - 100, 370);
            g.setColor(Color.white);
            if (PlaneWarPanel.level < 2) {
                g.drawString("[按R重新开始游戏]", UiConstant.APP_WIDTH / 2 - 130, 450);
            } else {
                g.drawString("[按R重新开始游戏]", UiConstant.APP_WIDTH / 2 - 130, 450);
                g.drawString("[按空格重新挑战本关]", UiConstant.APP_WIDTH / 2 - 130, 500);
            }
        }
    }

    //重新开始游戏
    public static boolean restartGame() {
        if (KeyStatesConfig.isKeyPressed(KeyEvent.VK_R)) {
            EndLogic.gameOver = false;
            EntityCrash.score = 0;
            gameWin = false;

            PlaneWarPanel.BgImage = ImageUtil.getImg("BG.png");
            PlaneWarPanel.level = 1;


        } else if (KeyStatesConfig.isKeyPressed(KeyEvent.VK_SPACE)) {
            EndLogic.gameOver = false;
            EntityCrash.score = 0;
            gameWin = false;
            PlaneWarPanel.BgImage = ImageUtil.getImg("BG2.png");
            PlaneWarPanel.level = 2;
        }

        return gameOver;
    }

    // 分数达到目标游戏胜利
    public static void win(int score) {
        if (score >= EndConstant.END_SCORE && !Boss.bossLiveStatus) {
            gameOver = true;
            gameWin = true;
        }
    }

    public static void gameWinUi(Graphics g) {
        if (gameOver && gameWin) {
            //设置画笔为红色
            g.setColor(Color.GREEN);
            Graphics2D g2d = (Graphics2D) g;
            //设置透明度,绘制红色矩形
            AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
            g2d.setComposite(alphaComposite);
            g2d.fillRect(0, 200, UiConstant.APP_WIDTH, 300);
            //透明度设置回来，然后设置成白色
            AlphaComposite alphaComposite1 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
            g2d.setComposite(alphaComposite1);
            g.setColor(Color.white);
            g.setFont(new Font("楷体", Font.BOLD, 30));
            g.drawString("游戏胜利", UiConstant.APP_WIDTH / 2 - 100, 240);
            g.setColor(Color.yellow);
            g.drawString("本次得分：" + EntityCrash.score, UiConstant.APP_WIDTH / 2 - 100, 300);
            g.drawString("获得金币：", UiConstant.APP_WIDTH / 2 - 70, 370);
            g.drawImage(PlaneWarPanel.MoneyImage, UiConstant.APP_WIDTH / 2 - 100, 340, 40, 40, null);
            g.drawString("0.00k", UiConstant.APP_WIDTH / 2 - 100, 370);
            g.setColor(Color.white);
            g.drawString("[按R重新开始游戏]", UiConstant.APP_WIDTH / 2 - 125, 450);
            if (PlaneWarPanel.level < 2) {
                g.drawString("[按空格进入下一关]", UiConstant.APP_WIDTH / 2 - 125, 500);
            } else {
                g.drawString("[按空格重新挑战本关]", UiConstant.APP_WIDTH / 2 - 125, 500);
            }
        }
    }
}


